package com.sap.jcp.sm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.sap.jcp.sm.domain.Track;

public class TrackDAOImpl implements TrackDAO {

	@SessionTarget
	Session session;
	@TransactionTarget
	Transaction transaction;

	@SuppressWarnings("unchecked")
	public List<Track> listTrack() {
		List<Track> trackList = null;
		try {
			//System.out.println("Listing tracks");
			trackList = session.createQuery("from Track").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return trackList;
	}

	public void saveTrack(Track track) {
		try {
			//System.out.println("Saving Track");
			session.save(track);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	
	public void updateTrack(Track track) {
		try {
			//System.out.println("update tracks");
			session.update(track);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	public void deleteTrack(Track track) {
		try {
			//System.out.println("delete track");
			session.delete(track);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	


	public Track getTrack(Long trackId) {
		
		String hql = "from Track where trackId = :trackId";
		Query query = session.createQuery(hql);
		query.setLong("trackId", trackId); 
		List<Track> trackList = query.list();
		if (trackList != null && !trackList.isEmpty()) {
			return trackList.get(0);
		}
		return null;
	}
}
